﻿using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using Sunny.UI;
using System;
using System.Windows.Forms;

namespace DevelopTool.Forms.DataConversionForms
{
    /// <summary>
    /// 时间戳转换
    /// </summary>
    public partial class TimestampConversionForm : BaseUIPage
    {

        private bool isGetNowTime = true;

        private bool isGetNowTimeSpan = true;

        private System.Windows.Forms.Timer timer;

        public TimestampConversionForm()
        {
            InitializeComponent();
        }

        private void TimestampConversionForm_Load(object sender, EventArgs e)
        {
            this.Disposed += this.TimestampConversionForm_Disposed;

            this.uiTextBoxNowTime.Text = DateTime.Now.String();
            this.uiTextBoxNowTimeSpan.Text = DateTime.Now.GetTotalSeconds().ToString();

            this.timer = new System.Windows.Forms.Timer
            {
                Interval = 1000,
                Enabled = true//启动计时器
            };
            timer.Tick += delegate (object ss, EventArgs ee)
            {
                DateTime dateTime = DateTime.Now;

                if (this.isGetNowTime)
                    this.uiTextBoxNowTime.Text = dateTime.String();

                if (this.isGetNowTimeSpan)
                    this.uiTextBoxNowTimeSpan.Text = dateTime.GetTotalSeconds().ToString();
            };
        }



        private void uiButtonNowTimeGet_Click(object sender, EventArgs e)
        {
            this.isGetNowTime = !this.isGetNowTime;
            if (this.uiButtonNowTimeGet.Text == "开始") this.uiButtonNowTimeGet.Text = "停止";
            else if (this.uiButtonNowTimeGet.Text == "停止") this.uiButtonNowTimeGet.Text = "开始";
        }

        private void uiButtonNowTimeSpanGet_Click(object sender, EventArgs e)
        {
            this.isGetNowTimeSpan = !this.isGetNowTimeSpan;
            if (this.uiButtonNowTimeSpanGet.Text == "开始") this.uiButtonNowTimeSpanGet.Text = "停止";
            else if (this.uiButtonNowTimeSpanGet.Text == "停止") this.uiButtonNowTimeSpanGet.Text = "开始";
        }

        private void uiButtonNowTimeCopy_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(this.uiTextBoxNowTime.Text);
            this.ShowSuccessNotifier("当前时间已复制");
        }

        private void uiButtonNowTimeSpanCopy_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(this.uiTextBoxNowTimeSpan.Text);
            this.ShowSuccessNotifier("当前时间戳已复制");
        }


        /// <summary>
        /// 时间戳转换为时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButtonTimeSpanConversion_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.uiTextBoxTimeSpanValue.Text))
                {
                    this.ShowWarningNotifier("请输入有效的时间戳文本");
                    return;
                }

                DateTime dateTime = DateTimeHelper.GetDateTime(this.uiTextBoxTimeSpanValue.Text);

                this.txtTimeDay.Text= dateTime.DateString();//天
                this.txtTimeHour.Text = dateTime.ToString("yyyy-MM-dd HH");//小时
                this.txtTimeMinute.Text = dateTime.ToString("yyyy-MM-dd HH:mm");//分钟
                this.txtTimeSecond.Text = dateTime.String();//秒
                this.txtTimeMillisecond.Text = dateTime.DateTimeExString();//毫秒

            }
            catch (Exception ex)
            {
                this.ShowWarningDialog(ex.Message);
            }

        }


        /// <summary>
        /// 时间转换为时间戳
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButtonTimeConversion_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.uiTextBoxTimeValue.Text))
                {
                    this.ShowWarningNotifier("请输入有效的时间日期文本");
                    return;
                }

                if (!DateTime.TryParse(this.uiTextBoxTimeValue.Text, out DateTime dateTime))
                {
                    this.ShowWarningNotifier("字符串不是有效的日期");
                    return;
                }

                this.txtTimeSpanDay.Text= dateTime.GetTotalDays().ToString();//天
                this.txtTimeSpanHour.Text = dateTime.GetTotalHours().ToString();//小时
                this.txtTimeSpanMinute.Text = dateTime.GetTotalMinutes().ToString();//分钟
                this.txtTimeSpanSecond.Text = dateTime.GetTotalSeconds().ToString();//秒
                this.txtTimeSpanMillisecond.Text = dateTime.GetTotalMilliseconds().ToString();//毫秒
                this.txtTimeSpanMicrosecond.Text = dateTime.GetTotalMicroseconds().ToString();//微秒
                this.txtTimeSpanNanosecond.Text = dateTime.GetTotalNanoseconds().ToString();//纳秒
            }
            catch (Exception ex)
            {
                this.ShowWarningDialog(ex.Message);
            }
        }

        private void TimestampConversionForm_Disposed(object sender,EventArgs e)
        {
            this.timer?.Dispose();
        }
    }
}
